import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  { path: '', redirect: '/home' },
  { name: 'login', path: '/login', component: () => import('@/views/login') },
  {
    name: 'chart',
    path: '/chart',
    component: () => import('@/views/find/chart')
  },
  {
    name: 'interview',
    path: '/interview',
    component: () => import('@/views/exam/interview')
  },
  { name: '404', path: '/404', component: () => import('@/views/404') },
  {
    name: 'article',
    path: '/article',
    component: () => import('@/views/find/modules/article')
  },
  {
    name: 'details',
    path: '/details',
    component: () => import('@/views/find/modules/details')
  },
  {
    name: 'shareList',
    path: '/shareList',
    component: () => import('@/views/find/modules/shareList')
  },
  {
    name: 'shareDetail',
    path: '/shareDetail',
    component: () => import('@/views/find/modules/shareDetail')
  },
  {
    name: 'mySearch',
    path: '/mySearch',
    component: () => import('@/views/find/modules/mySearch')
  },
  {
    name: 'layout',
    path: '/layout',
    component: () => import('@/views/layout'),
    children: [
      {
        name: 'home',
        path: '/home',
        component: () => import('@/views/home'),
        meta: { needKeep: true }
      },
      {
        name: 'exam',
        path: '/exam',
        component: () => import('@/views/exam')
      },
      {
        name: 'find',
        path: '/find',
        component: () => import('@/views/find/index.vue')
      },
      {
        name: 'mine',
        path: '/mine',
        component: () => import('@/views/mine/index.vue'),
        meta: {
          needLogin: true
        }
      }
    ]
  },

  {
    name: 'edit',
    path: '/edit',
    component: () => import('@/views/mine/edit.vue'),
    meta: {
      needLogin: true
    }
  },
  {
    name: 'avatar',
    path: '/avatar',
    component: () => import('@/views/mine/avatar.vue')
  },
  {
    name: 'nickname',
    path: '/nickname',
    component: () => import('@/views/mine/nickname.vue')
  },
  {
    name: 'area',
    path: '/area',
    component: () => import('@/views/mine/area.vue')
  },
  {
    name: 'gangwei',
    path: '/gangwei',
    component: () => import('@/views/mine/gangwei.vue')
  },
  {
    name: 'message',
    path: '/message',
    component: () => import('@/views/mine/message.vue')
  },

  // 公司详情页面
  {
    name: 'companyDetail',
    path: '/companyDetail/:id',
    component: () => import('@/views/home/companyDetail')
  },
  // 在招职位页面
  {
    name: 'companyPosition',
    path: '/companyPosition/:id',
    component: () => import('@/views/home/companyPosition')
  }

  // { path: '*', redirect: '/404' }
]

const router = new VueRouter({
  routes
})

// 设置路由前置守卫
router.beforeEach((to, from, next) => {
  // console.log(to)

  // 判断有没有token
  if (store.state.user.token) {
    // 有token
    if (to.path === '/login') {
      // 去的是登录页，返回首页
      Vue.prototype.$toast.fail('你已登录')
    } else {
      // 去得不是登录页,放行
      next()
    }
  } else {
    // 无token，
    if (to.meta.needLogin) {
      // 需要token访问，返回登录页
      Vue.prototype.$toast.fail('请先登录')
      next({
        name: 'login',
        query: {
          back: to.path
        }
      })
    } else {
      // 没token,也不需要token访问
      next()
    }
  }
})

export default router
